package com.zhengdongzulin.zuke.app.main.mine.contract

import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.LogUtils
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.flowlayout.DefaultFlowTagAdapter
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ContractListData
import com.zhengdongzulin.zuke.app.main.ZKWebviewActivity
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityMyContractBinding
import com.zhengdongzulin.zuke.ext.*
import net.lucode.hackware.magicindicator.buildins.UIUtil


/**
 * jeff
 * 我的合同
 * 2021-03-27 17:50:47
 */
class ZKMyContractActivity : BaseActivity<MyContractVm, ZkActivityMyContractBinding>() {

    var mData = mutableListOf<ContractListData>()
    var mStatusColors = HashMap<String, StatusColors>()

    override fun getLayoutResId(): Int = R.layout.zk_activity_my_contract

    override fun initVM(): MyContractVm = getVM(MyContractVm::class.java)

    override fun initView() {
        setTitleContent("我的合同")
        initStatusColors()
        mView.contentView.setEnableRefresh(false)

        if (intent.hasExtra("id") && !TextUtils.isEmpty(intent.getStringExtra("id"))) {
            LogUtils.e(intent.getStringExtra("id"))
            mViewModel.list_house_contract(intent.getStringExtra("id")!!)
        }
    }

    override fun initData() {
        mView.recyclerView.vertical()
            .bindData(mData, R.layout.item_contract) { holder, item, position ->
                val name = holder.getView<TextView>(R.id.name)
                val flow = holder.getView<FlowTagLayout>(R.id.flow)
                when (item.houseType) {
                    "3" -> {
                        name.visible()
                        flow.gone()
                        name.text = "${item.houseNumber}"
                    }
                    else -> {
                        name.gone()
                        if (item.houseFeature != null && item.houseFeature.size > 0) {
                            flow.setAdapter(object : DefaultFlowTagAdapter(this) {
                                override fun getLayoutId(): Int {
                                    return R.layout.item_find_house_list_flow
                                }
                            })
                            flow.setItems(item.houseFeature)
                            flow.visible()
                        } else {
                            flow.gone()
                        }
                    }
                }
                when (item.contractStatus) {
                    "0" -> {
                        holder.setText(R.id.tv_contract_time, "最晚支付时间\u0020${item.latestPayTime}")
                    }
                    "1" -> {
                        holder.setText(R.id.tv_contract_time, "最晚签约时间\u0020${item.latestSignTime}")
                    }
                    else -> {
                        holder.setText(
                            R.id.tv_contract_time,
                            "${item.startDate}\u0020至\u0020${item.endDate}"
                        )
                    }
                }


                var statusColor = mStatusColors[item.contractStatus]!!

                var money = holder.getView<TextView>(R.id.tv_contract_money)
                money.text = "￥${item.rentPrice}"
                money.appendSizeSpan("/月", 0.6f)

                holder.getView<View>(R.id.v_bg).background =
                    resources.getDrawable(statusColor.bgColor)
                holder.getView<TextView>(R.id.tv_contract_title_name)
                    .setTextColor(statusColor.textColor)
                holder.getView<TextView>(R.id.tv_contract_time).setTextColor(statusColor.textColor)
                holder.getView<TextView>(R.id.tv_contract_money).setTextColor(statusColor.textColor)
                holder.setImageResource(R.id.iv_contract_status, statusColor.statusRes)
                var bt1 = holder.getView<TextView>(R.id.bt1).apply {
                    setTextColor(statusColor.textColor)
                    setBackgroundResource(R.drawable.item_contract_storke)
                    (background as GradientDrawable).setStroke(
                        UIUtil.dip2px(
                            this@ZKMyContractActivity,
                            1.0
                        ), statusColor.textColor
                    )
                }
                var bt2 = holder.getView<TextView>(R.id.bt2).apply {
                    setTextColor(statusColor.textColor)
                    setBackgroundResource(R.drawable.item_contract_storke)
                    (background as GradientDrawable).setStroke(
                        UIUtil.dip2px(
                            this@ZKMyContractActivity,
                            1.0
                        ), statusColor.textColor
                    )
                }

                //合同状态 0 待支付 1. 待签字 2 待审核 4 待入住 5 在租中 7 已违约 8 已退租
                bt1.visibility = View.VISIBLE
                when (item.contractStatus) {
                    "0" -> {
                        bt1.text = "立即支付"
                    }
                    "1" -> {
                        bt1.text = "在线签字"
                    }
                    "4" -> {
                        bt1.text = "查看合同"
                    }
                    else -> {
                        bt1.visibility = View.GONE
                    }
                }

                bt1.setOnClickListener {
                    when (item.contractStatus) {
                        "0" -> { //支付

                        }
                        "1" -> { //签约
                            if (!item.contractAddress.isNullOrEmpty()) {
                                Intent().put("url", item.contractAddress).put("title", "在线签字")
                                    .start(
                                        this@ZKMyContractActivity,
                                        ZKWebviewActivity::class.java
                                    )
                            } else {
                                toast("签约url为空")
                            }
                        }
                        "4" -> { //查看合同
                            Intent().put("id", "${item.contractId}")
                                .start(
                                    this@ZKMyContractActivity,
                                    ZKContractDetailsActivity::class.java
                                )
                        }
                    }
                }
                bt2.setOnClickListener {//联系管家
                    callPhoneDialog(item.housekeeperName, item.housekeeperPhone).show()
                }
                holder.getView<ImageView>(R.id.img)
                    .loadImage(item.housePicUrl, R.mipmap.error_empty_zheng, 4f)
                holder.setText(
                    R.id.desc,
                    "${item.doorName}\u0020\u0020${item.area}㎡\u0020\u0020${item.orientation}"
                )
                holder.setText(R.id.address, "${item.houseAddress}")
                holder.setText(R.id.tv_contract_title_name, "${item.houseName}")
            }.itemClick { data: List<ContractListData>, holder, position ->
                Intent().put("id", data[position].contractId + "")
                    .start(this@ZKMyContractActivity, ZKContractDetailsActivity::class.java)
            }

    }

    data class StatusColors(
        var textColor: Int,
        var bgColor: Int,
        var statusRes: Int
    )

    private fun initStatusColors() {
        //0 待支付 1. 待签字 2 待审核 4 待入住 5 在租中 7 已违约 8 已退租
        mStatusColors["0"] = StatusColors(
            R.color.contract_status_0,
            R.color.contract_status_bg_0,
            R.mipmap.contract_status_0
        )
        mStatusColors["1"] = StatusColors(
            R.color.contract_status_1,
            R.color.contract_status_bg_1,
            R.mipmap.contract_status_1
        )
        mStatusColors["2"] = StatusColors(
            R.color.contract_status_2,
            R.color.contract_status_bg_2,
            R.mipmap.contract_status_2
        )
        mStatusColors["4"] = StatusColors(
            R.color.contract_status_4,
            R.color.contract_status_bg_4,
            R.mipmap.contract_status_4
        )
        mStatusColors["5"] = StatusColors(
            R.color.contract_status_5,
            R.color.contract_status_bg_5,
            R.mipmap.contract_status_5
        )
        mStatusColors["7"] = StatusColors(
            R.color.contract_status_7,
            R.color.contract_status_bg_7,
            R.mipmap.contract_status_7
        )
        mStatusColors["8"] = StatusColors(
            R.color.contract_status_8,
            R.color.contract_status_bg_8,
            R.mipmap.contract_status_8
        )
    }

    override fun initListener() {

    }

    override fun startObserve() {
        mViewModel.list_house_contract.observe(this, androidx.lifecycle.Observer {
            mData.clear()
            if (it != null && it.size > 0) {
                mData.addAll(it)
                mView.recyclerView.adapter!!.notifyDataSetChanged()
            }
            mView.statusView.showContent()
        })
        mViewModel.list_house_contract.state.observe(
            this,
            getObserver(mViewModel.list_house_contract)
        )
    }

}